package fr.greta.centre.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;

import fr.greta.centre.metier.ActionsFormation;
import fr.greta.centre.metier.Module;

public class ModuleDAOImpl implements ModuleDAO {

	private Statement st;
	
	
	/**
	 * Ne faut-t-il pas sauver les actionsFormation du module dans cette methode?
	 */
	public int save(Module unModule) {
		int i=0;
		try{
			st=Bd.getConnect().createStatement();
			String s="INSERT INTO `centre_formation`.`module` (id_module, nom_module, code, coefficient)" +
					" VALUES ("+unModule.getIdModule()+", "+unModule.getNom()+", "+unModule.getCode()+
					", "+unModule.getCoeff()+")";
		    System.out.println(s);
	        st.executeUpdate(s);
	        

			String ss = "SELECT MAX( id_Module) AS LastID FROM module";
			ResultSet r = st.executeQuery(ss);
			r.next();
			i = r.getInt(1);
			st.getConnection().close();
		} catch (SQLException e) {	System.out.println(e.getMessage());	}
		
		System.out.println("insert ok"+" "+i);
		unModule.setIdModule(i);		
		return i;
	}

	
	public boolean delete(int id)  {
		try {
			st = Bd.getConnect().createStatement();
			String s = "DELETE FROM `centre_formation`.`module` WHERE `module`.`id_module` = " + id;
			System.out.println(s);
			st.execute(s);
			st.getConnection().close();
		} catch (SQLException e) { System.out.println(e.getMessage());

		}

	
		
		return false;
	}

	
	public Module findByKey(int id)  {
		Module unModule;
		try {
			st = Bd.getConnect().createStatement();
			String s = "SELECT * FROM `centre_formation`.`module` WHERE `module`.`id_module` = " +id ;
			System.out.println(s);
			st.execute(s);
			ResultSet r = st.executeQuery(s);
			r.next();
			List<ActionsFormation> lesActionsFormations= new ActionsFormationDAOImpl().findByIdModule(r.getInt("id_module"));
			unModule = new Module(r.getInt("id_module"), r.getString("nom_module"), r.getString("code"), r.getInt("coefficient"), lesActionsFormations);
			st.getConnection().close();

		} catch (SQLException e) {System.out.println(e.getMessage());
		return null;
		}

		
		return unModule;
	}


	public List<Module> findAll() {
		ArrayList<Module> lesModules = new ArrayList<Module>();

		try {
			Statement st = Bd.getConnect().createStatement();
			String sql = "SELECT * FROM module";
			ResultSet rs = st.executeQuery(sql);
			ActionsFormationDAOImpl uneActionFormationDAOImpl = new ActionsFormationDAOImpl();
			
			while(rs.next()){
				Module unModule = new Module(rs.getInt("id_module"), rs.getString("nom_module"),
						rs.getString("code"), rs.getInt("coefficient"),
						uneActionFormationDAOImpl.findByIdModule(rs.getInt("id_module")));
				lesModules.add(unModule);
			}
			st.getConnection().close();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		
		return lesModules;
	}

	public List<Module> findByIdUe(int idUE){
		//TODO
		return null;
	}
}
